--使用方法：直接粘贴到模组环境 使用ClearDB方法 第一个参数是实体

local function ClearBadEgg(smlval, tval, nz)
    if smlval == nil or smlval._modifiers == nil then return end 
    if nz then
        for source, src_params in pairs(smlval._modifiers) do
            for k, v in pairs(src_params.modifiers) do
                if v > tval then
                    v = tval
                end
            end
        end
    else
        for source, src_params in pairs(smlval._modifiers) do
            for k, v in pairs(src_params.modifiers) do
                if v < tval then
                    v = tval
                end
            end
        end
    end
end

function ClearDB(inst)
    KillThreadsWithID(inst.GUID)
    if inst.components.freezable ~= nil and inst.components.freezable:IsFrozen() then
        inst.components.freezable:Unfreeze()
    end
    if inst.components.pinnable ~= nil and inst.components.pinnable:IsStuck() then
        inst.components.pinnable:Unstick()
    end
    if inst.components.burnable ~= nil then
        inst.components.burnable:Extinguish()
    end

    local inkable = inst.components.inkable
    if inkable ~= nil and inkable.inktime > 0 then
        inkable.inktime = 0
        inkable.inked = nil
    end

    if inst.components.combat ~= nil then
        ClearBadEgg(inst.components.combat.externaldamagemultipliers, 1)
        ClearBadEgg(inst.components.combat.externaldamagetakenmultipliers, 1, true)
    end

    if inst.components.inventory and not inst:HasTag("wereplayer") then
        inst.components.inventory:Open()
        inst.components.inventory:Show()
        inst:ShowActions(true)
    end
    if inst.components.playercontroller ~= nil then
        inst.components.playercontroller:EnableMapControls(true)
        inst.components.playercontroller:Enable(true)
    end

    local locomotor = inst.components.locomotor
    if locomotor ~= nil then
        for source, t in pairs(locomotor._externalspeedmultipliers) do
            for key,m in pairs(t.multipliers) do
                if m < 1 then
                    locomotor:RemoveExternalSpeedMultiplier(source, key)
                end
            end
        end
    end

    local health = inst.components.health
    if health ~= nil then
        if health:IsDead() then
            return
        end
        if health.regen ~= nil and health.regen.amount and health.regen.amount < 0 then
            health:StopRegen()
        end
        if health.regensources ~= nil then
            local keytable = {}
            for source, rdata in pairs(health.regensources) do
                for key, v in pairs(rdata.tasks) do
                    if v.amount and v.amount < 0 then
                        table.insert(keytable, source)
                        break
                    end
                end
            end
            --TheNet:Say(#keytable)
            if #keytable > 0 then
                for k, v in pairs(keytable) do
                    health:RemoveRegenSource(v)
                end
            end
            keytable = nil
        end
        if health.absorb < 0 then
            health.absorb = 0
        end
        ClearBadEgg(health.externalabsorbmodifiers, 0)
        ClearBadEgg(health.externalfiredamagemultipliers, 1, true)
    end

    --------------------------------------------

    if inst.sg and inst.sg:HasState("idle") and (inst.sg:HasStateTag("busy") or inst.sg:HasStateTag("sleeping")) then
        inst.sg:GoToState("idle")
    end

end